package lmteam.commservice;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

// mail code was developed with help from the following website
//http://www.javapractices.com/topic/TopicAction.do?Id=144


public class CommServiceProcessor
    implements ICommunication
{

    private Properties mailProps;

    public CommServiceProcessor()
    {
	mailProps = new Properties();

	mailProps.setProperty( "mail.host", "fakesystem.com" );
	mailProps.setProperty( "mail.from", "webmaster@fakesystem.com" );


    }

    // webservice inherited function sendForReview
    public boolean sendForReview(String senderName, 
				 String senderAddr, 
				 String[] receiverNames, 
				 String[] receiverAddrs, 
				 String docname, 
				 String extraBody ) 
	throws java.rmi.RemoteException
    {
	// this is considered an error
	if ( receiverNames.length == 0 ||
	     receiverNames.length != receiverAddrs.length )
	    return false;

	int numpeop = receiverNames.length;
	int i;
	boolean output;
	output = true;
	for ( i = 0; i < numpeop; ++i )
	    {
		String sendaddr, recvaddr, subject, body;

		sendaddr = senderAddr;
		recvaddr = receiverAddrs[i];
		subject = "Document Review Request for document named " + docname;

		body = createReviewMessage( senderName, 
					    receiverNames[i],
					    docname, 
					    extraBody );

		output = output && sendEmail( sendaddr, 
					      recvaddr,
					      subject,
					      body );
	    }

	return output;
    }

    // webservice inherited function sendComment
    public boolean sendComment(String senderName, 
			       String senderAddr, 
			       String receiverName, 
			       String receiverAddr, 
			       String docname, 
			       String extraBody ) 
	throws java.rmi.RemoteException
    {
	String subject, body;
	
	subject = "Document Comment entered for document named " + docname;
	
	body = createCommentMessage( senderName, 
				     receiverName,
				     docname, 
				     extraBody );
	
	return sendEmail( senderAddr, 
			  receiverAddr,
			  subject,
			  body );
    }

    // webservice inherited function sendCommentResponse
    public boolean sendCommentResponse(String senderName, 
				       String senderAddr, 
				       String receiverName, 
				       String receiverAddr, 
				       String docname, 
				       String extraBody ) 
	throws java.rmi.RemoteException
    {
	String subject, body;
	
	subject = "Document Comment Response entered for document named " + docname;
	
	body = createCommentMessage( senderName, 
				     receiverName,
				     docname, 
				     extraBody );
	
	return sendEmail( senderAddr, 
			  receiverAddr,
			  subject,
			  body );
    }

    // webservice inherited function sendGeneralEmail
    public boolean sendGeneralEmail(String senderName, 
				    String senderAddr, 
				    String[] receiverNames, 
				    String[] receiverAddrs, 
				    String subject, 
				    String body ) 
	throws java.rmi.RemoteException
    {
	// this is considered an error
	if ( receiverNames.length == 0 ||
	     receiverNames.length != receiverAddrs.length )
	    return false;

	int numpeop = receiverNames.length;
	int i;
	boolean output;
	output = true;
	for ( i = 0; i < numpeop; ++i )
	    {
		String sendaddr, recvaddr;

		sendaddr = senderAddr;
		recvaddr = receiverAddrs[i];

		output = output && sendEmail( sendaddr, 
					      recvaddr,
					      subject,
					      body );
	    }

	return output;
    }

    // simple function to do the actually mail sending
    protected boolean sendEmail( String sendaddr, String recvaddr, String subject, String body )
    {
	Session session = Session.getDefaultInstance( mailProps, null );
	MimeMessage message = new MimeMessage( session );

	try
	    {
		message.setFrom( new InternetAddress( sendaddr ) );
		message.addRecipient( Message.RecipientType.TO, new InternetAddress(recvaddr) );

		message.setSubject( subject );
		message.setText( body );
		
		
		// transport is shut off since this project isn't running a real webserver
		//Transport.send( message );

	    }
	catch ( MessagingException me )
	    {
		me.printStackTrace();
		return false;
	    }
	

	return true;
    }


    // the following three routines are canned email messages
    // for three of the special use interface functions. 

    private final String finalpart1 = "Sincerely,\nLMTEAM Website (on behalf of ";
    private final String finalpart2 = ")\n";

    private final String reviewpart1 = "We are pleased to invite you to review ";
    private final String reviewpart2 = " on behalf of "; 
    private final String reviewpart3 = "Please log in at your earliest convenience to review this document.\n\n";
    protected String createReviewMessage( String sname, String rname, String docname, String extra )
    {
	String out;

	out = "Dear " + rname + ",\n\n" + reviewpart1 + 
	    docname + reviewpart2 + sname + ". " + reviewpart3 + 
	    "\n\n" + extra + "\n\n" + finalpart1 + sname + 
	    finalpart2;
	
	return out;
    }

    private final String commentpart1 = "We are pleased to inform you that a comment has been written against your document ";
    private final String commentpart2 = " by ";
    private final String commentpart3 = "Please log in at your earliest convenience to review this comment.\n\n";
    protected String createCommentMessage( String sname, String rname, String docname, String extra )
    {
	String out;
	out = "Dear " + rname + ",\n\n" + commentpart1 + 
	    docname + commentpart2 + sname + ". " + 
	    commentpart3 + "\n\n" + extra + "\n\n" + 
	    finalpart1 + sname + finalpart2;
	return out;
    }

    private final String rcommpart1 = "We are pleased to inform you that a comment you wrote has been responded to by ";
    private final String rcommpart3 = "Please log in at your earliest convenience to review this comment.\n\n";
    protected String createCommentResponseMessage( String sname, String rname, String docname, String extra )
    {
	String out;

	out = "Dear " + rname + ",\n\n" + rcommpart1 + 
	    sname + ". " + rcommpart3 + "\n\n" + extra + 
	    "\n\n" + finalpart1 + sname + finalpart2;

	return out;
    }
}